using System;
using System.Collections.Generic;
using System.Text;
using Csla;

namespace Templates
{
  [Serializable()]
  class EditableRoot : BusinessBase<EditableRoot>
  {
    #region Business Methods

    // TODO: add your own fields, properties and methods
    private int _id;

    /// <summary>
    /// Gets or sets the id.
    /// </summary>
    /// <value>The id.</value>
    public int Id
    {
      [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
      get 
      {
        CanReadProperty(true);
        return _id; 
      }
      [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
      set
      {
        CanWriteProperty(true);
        if (_id != value)
        {
          _id = value;
          PropertyHasChanged();
        }
      }
    }

    /// <summary>
    /// Override this method to return a unique identifying
    /// value for this object.
    /// </summary>
    /// <returns></returns>
    /// <remarks>
    /// If you can not provide a unique identifying value, it
    /// is best if you can generate such a unique value (even
    /// temporarily). If you can not do that, then return
    /// <see langword="Nothing"/> and then manually override the
    /// <see cref="Equals"/>, <see cref="GetHashCode"/> and
    /// <see cref="ToString"/> methods in your business object.
    /// </remarks>
    protected override object GetIdValue()
    {
      return _id;
    }

    #endregion

    #region Validation Rules

    /// <summary>
    /// Override this method in your business class to
    /// be notified when you need to set up shared
    /// business rules.
    /// </summary>
    /// <remarks>
    /// This method is automatically called by CSLA .NET
    /// when your object should associate per-type
    /// validation rules with its properties.
    /// </remarks>
    protected override void AddBusinessRules()
    {
      // TODO: add validation rules
      //ValidationRules.AddRule(null, "");
    }

    #endregion

    #region Authorization Rules

    /// <summary>
    /// Override this method to add per-type
    /// authorization rules for your type's properties.
    /// </summary>
    /// <remarks>
    /// AddAuthorizationRules is automatically called by CSLA .NET
    /// when your object should associate per-type authorization roles
    /// with its properties.
    /// </remarks>
    protected override void AddAuthorizationRules()
    {
      // TODO: add authorization rules
      //AuthorizationRules.AllowWrite("", "");
    }

    /// <summary>
    /// Determines whether this instance [can add object].
    /// </summary>
    /// <returns>
    /// 	<c>true</c> if this instance [can add object]; otherwise, <c>false</c>.
    /// </returns>
    public static bool CanAddObject()
    {
      // TODO: customize to check user role
      //return ApplicationContext.User.IsInRole("");
      return true;
    }

    /// <summary>
    /// Determines whether this instance [can get object].
    /// </summary>
    /// <returns>
    /// 	<c>true</c> if this instance [can get object]; otherwise, <c>false</c>.
    /// </returns>
    public static bool CanGetObject()
    {
      // TODO: customize to check user role
      //return ApplicationContext.User.IsInRole("");
      return true;
    }

    /// <summary>
    /// Determines whether this instance [can edit object].
    /// </summary>
    /// <returns>
    /// 	<c>true</c> if this instance [can edit object]; otherwise, <c>false</c>.
    /// </returns>
    public static bool CanEditObject()
    {
      // TODO: customize to check user role
      //return ApplicationContext.User.IsInRole("");
      return true;
    }

    /// <summary>
    /// Determines whether this instance [can delete object].
    /// </summary>
    /// <returns>
    /// 	<c>true</c> if this instance [can delete object]; otherwise, <c>false</c>.
    /// </returns>
    public static bool CanDeleteObject()
    {
      // TODO: customize to check user role
      //return ApplicationContext.User.IsInRole("");
      return true;
    }

    #endregion

    #region Factory Methods

    /// <summary>
    /// News the editable root.
    /// </summary>
    /// <returns></returns>
    public static EditableRoot NewEditableRoot()
    {
      return DataPortal.Create<EditableRoot>();
    }

    /// <summary>
    /// Gets the editable root.
    /// </summary>
    /// <param name="id">The id.</param>
    /// <returns></returns>
    public static EditableRoot GetEditableRoot(int id)
    {
      return DataPortal.Fetch<EditableRoot>(new Criteria(id));
    }

    /// <summary>
    /// Deletes the editable root.
    /// </summary>
    /// <param name="id">The id.</param>
    public static void DeleteEditableRoot(int id)
    {
      DataPortal.Delete(new Criteria(id));
    }

    private EditableRoot()
    { /* Require use of factory methods */ }

    #endregion

    #region Data Access

    [Serializable()]
    private class Criteria
    {
      private int _id;
      public int Id
      {
        get { return _id; }
      }
      public Criteria(int id)
      { _id = id; }
    }

    /// <summary>
    /// Override this method to load a new business object with default
    /// values from the database.
    /// </summary>
    /// <remarks>
    /// Normally you will overload this method to accept a strongly-typed
    /// criteria parameter, rather than overriding the method with a
    /// loosely-typed criteria parameter.
    /// </remarks>
    protected override void DataPortal_Create()
    {
      // TODO: load default values
    }

    /// <summary>
    /// Datas the portal_ fetch.
    /// </summary>
    /// <param name="criteria">The criteria.</param>
    private void DataPortal_Fetch(Criteria criteria)
    {
      // TODO: load values
    }

    /// <summary>
    /// Override this method to allow insertion of a business
    /// object.
    /// </summary>
    protected override void DataPortal_Insert()
    {
      // TODO: insert values
    }

    /// <summary>
    /// Override this method to allow update of a business
    /// object.
    /// </summary>
    protected override void DataPortal_Update()
    {
      // TODO: update values
    }

    /// <summary>
    /// Override this method to allow deferred deletion of a business object.
    /// </summary>
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new Criteria(_id));
    }

    /// <summary>
    /// Datas the portal_ delete.
    /// </summary>
    /// <param name="criteria">The criteria.</param>
    private void DataPortal_Delete(Criteria criteria)
    {
      // TODO: delete values
    }

    #endregion
  }
}
